package org.jeecg.modules.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.bouncycastle.util.Arrays;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.utils.SaveAndLoadUtil;
import org.jeecg.modules.wechat.entity.WxUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO:
 *
 * @author Zhang Hao yu
 * @date 2020/12/23 14:20
 */
@RestController
@RequestMapping("/phil/common")
public class PhilCommonController {
    private String hostIP="localhost";
    private String userName = "root";
    private String passWord = "Abc123456$";
    private String databaseName = "jeecg-phil";
    private String savePath = "./opt/data";
    private String fPath = "./opt/data/";

    /**
     * TODO: 数据库备份
     *
     *
     * @author ZhangHaoyu
     * @param
     * @return org.jeecg.common.api.vo.Result<?>
     */
    @GetMapping(value = "save")
    public Result<?> save(){
        String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")).toString();
        System.out.println(fileName);
        boolean res = SaveAndLoadUtil.backup(savePath, fileName, hostIP, userName, passWord, databaseName);
        return Result.OK(res?"备份成功":"备份失败");
    }

    /**
     * TODO: 数据库还原
     *
     *
     * @author ZhangHaoyu
     * @param
     * @return org.jeecg.common.api.vo.Result<?>
     */
    @GetMapping(value = "load")
    public Result<?> load(@RequestParam(name = "fileName") String fileName){
        String res = SaveAndLoadUtil.load(fPath+fileName, hostIP, userName, passWord, databaseName);
        return Result.OK(res);
    }

    /**
     * TODO: 获取文件下所有备份的名称
     *
     *
     * @author ZhangHaoyu
     * @param
     * @return org.jeecg.common.api.vo.Result<?>
     */
    @GetMapping(value = "readName")
    public Result<?> readName(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                              @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        Page<FileVO> page = new Page<FileVO>(pageNo, pageSize);
        List<FileVO> fileVOS = new ArrayList<>();
        IPage<FileVO> pageList = page.setRecords(fileVOS);
        String[] list=new File(savePath).list();
        if (list==null){
            return Result.error("当前无备份");
        }
        for (String s : list) {
            FileVO fileVO = new FileVO();
            fileVO.fileName = s;
            fileVOS.add(fileVO);
        }
        return Result.OK(pageList);
    }

    private static class FileVO implements Serializable {
        private String fileName;

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
    }

    /**
     * TODO: sql删除
     *
     *
     * @author ZhangHaoyu
     * @param
     * @return org.jeecg.common.api.vo.Result<?>
     */
    @GetMapping(value = "delete")
    public Result<?> delete(@RequestParam(name = "fileName") String fileName){
        boolean res = SaveAndLoadUtil.deleteFile(fPath+fileName);
        return Result.OK(res?"删除成功":"删除失败");
    }
}